package com.example.administrator.mymusic1;

import android.media.MediaExtractor;
import android.media.MediaPlayer;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.jar.Manifest;

public class MainActivity extends AppCompatActivity {

    private ArrayList<String> geQuArray = new ArrayList<>();
    private int currentIndex = 0;
    private Handler handler;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView kaishi = (TextView) findViewById(R.id.bt_play);
        TextView zhanTin = (TextView) findViewById(R.id.Bt_pause);
        TextView huifang = (TextView) findViewById(R.id.bt_replay);
        TextView tinzhi = (TextView) findViewById(R.id.bt_stop);

        //进度条
        final SeekBar jindutiao = (SeekBar) findViewById(R.id.seekBar1);

        //路径框
        EditText lujing = (EditText) findViewById(R.id.et_inputpath);

        //上下曲

        TextView shangyiqu = (TextView) findViewById(R.id.shang);
        TextView xai = (TextView) findViewById(R.id.xia);

        //歌曲路径
        final MediaPlayer mediaPlayer = new MediaPlayer();
        //为播放器添加一个完成播放监听器
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mediaPlayer.stop();
                mediaPlayer.reset();

                currentIndex++;
                if (currentIndex == geQuArray.size()) {
                    currentIndex = 0;
                }

                try {
                    mediaPlayer.setDataSource(geQuArray.get(currentIndex));
                    mediaPlayer.prepare();

                } catch (IOException e) {
                }
                mediaPlayer.start();
            }
        });

        //  String defaultPath = Environment.getExternalStorageDirectory().getAbsolutePath();

        //拿到了预先设好目录
        try {
            ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE,
                    android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 124);

            File mulu = new File("/sdcard/KuwoMusic/music"); //   /sdcard/MP3/
            File[] files = mulu.listFiles();
            for (File f : files) {
                String fPath = f.getPath();
                if (fPath.contains("mp3")) {
                    geQuArray.add(fPath);
                }
                Toast.makeText(this, "正在播放"+fPath +"/*", Toast.LENGTH_LONG);

            }
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "无法找到路径", Toast.LENGTH_LONG);
        }

        //设置即将播放的文件
        try {
            mediaPlayer.setDataSource(geQuArray.get(currentIndex));//  /mnt/sd/mp3/...
            //准备播放
            mediaPlayer.prepare();
        } catch (IOException e) {

        }


        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                int total = msg.arg1;

                int current = msg.arg2;

                jindutiao.setMax(total);
                jindutiao.setProgress(current);
            }
        };

        jindutiao.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser == true) {
                    mediaPlayer.seekTo(progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        Thread thread = new Thread() {
            @Override
            public void run() {
                super.run();

                while (true) {
                    if (mediaPlayer != null && mediaPlayer.isPlaying()) {
                        int currentTime = mediaPlayer.getCurrentPosition();
                        int totalTime = mediaPlayer.getDuration();
                        Message message = new Message();
                        message.arg1 = totalTime;
                        message.arg2 = currentTime;
                        handler.sendMessage(message);
                    }

                    try {
                        sleep(100);
                    } catch (InterruptedException e) {
                        //
                    }


                }

            }
        };
        thread.start();


        kaishi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                mediaPlayer.start();

            }
        });

        zhanTin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mediaPlayer.pause();
            }
        });

        huifang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mediaPlayer.seekTo(0);
            }
        });

        tinzhi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mediaPlayer.stop();
                try {
                    mediaPlayer.prepare();
                    mediaPlayer.seekTo(0);

                } catch (IOException e) {

                }
            }
        });

        shangyiqu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //1. 停止当前播放的歌曲
                mediaPlayer.stop();

                //2. 还原播放器
                mediaPlayer.reset();

                //3. 防止上一曲到-1
                currentIndex = currentIndex - 1;
                if (currentIndex < 0) {
                    currentIndex = geQuArray.size() - 1;
                }

                //4. 重新设定播放源, 准备播放
                try {
                    mediaPlayer.setDataSource(geQuArray.get(currentIndex));
                    mediaPlayer.prepare();
                } catch (IOException e) {
                }

                //播放
                mediaPlayer.start();
            }
        });
        //下一曲
        xai.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mediaPlayer.stop();
                mediaPlayer.reset();

                currentIndex++;
                if (currentIndex == geQuArray.size()) {
                    currentIndex = 0;
                }

                try {
                    mediaPlayer.setDataSource(geQuArray.get(currentIndex));
                    mediaPlayer.prepare();

                } catch (IOException e) {
                }
                mediaPlayer.start();
            }
        });
    }
    public int miao (int haomiao){
        return  haomiao / 1000;
    };
    public int fen (int fen){
        return  fen / 60;
    };
    public int fenh (int haomiao){
        return( haomiao  / 1000) /60;
    };
    public int fenmiao (int miao){
        return  miao % 60;
    };

}
